<?php

/* SVN FILE: $Id: app_model.php 4409 2007-02-02 13:20:59Z phpnut $ */
/**
 * Application model for Cake.
 *
 * This file is application-wide model file. You can put all
 * application-wide model-related methods here.
 *
 * PHP versions 4 and 5
 *
 * CakePHP(tm) :  Rapid Development Framework <http://www.cakephp.org/>
 * Copyright 2005-2007, Cake Software Foundation, Inc.
 *								1785 E. Sahara Avenue, Suite 490-204
 *								Las Vegas, Nevada 89104
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @filesource
 * @copyright		Copyright 2005-2007, Cake Software Foundation, Inc.
 * @link				http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
 * @package			cake
 * @subpackage		cake.cake
 * @since			CakePHP(tm) v 0.2.9
 * @version			$Revision: 4409 $
 * @modifiedby		$LastChangedBy: phpnut $
 * @lastmodified	$Date: 2007-02-02 07:20:59 -0600 (Fri, 02 Feb 2007) $
 * @license			http://www.opensource.org/licenses/mit-license.php The MIT License
 */
/**
 * Application model for Cake.
 *
 * This is a placeholder class.
 * Create the same file in app/app_model.php
 * Add your application-wide methods to the class, your models will inherit them.
 *
 * @package		cake
 * @subpackage	cake.cake
 */
class AppModel extends Model {
	static $utf8IsSet = false;

	function __construct(){
		if(!self::$utf8IsSet) {
			$this->execute("SET NAMES 'utf8'");
			self::$utf8IsSet = true;
		}
		parent::__construct();
	}

	// basée sur http://cakebaker.wordpress.com/2006/02/06/yet-another-data-validation-approach/
	function invalidFields($data = array ()) {
		if (!$this->beforeValidate()) {
			return false;
		}

		if (!isset ($this->validate) || !empty ($this->validationErrors)) {
			if (!isset ($this->validate)) {
				return true;
			} else {
				return $this->validationErrors;
			}
		}

		if (isset ($this->data)) {
			$data = array_merge($data, $this->data);
		}

		$errors = array ();
		$this->set($data);

		foreach ($data as $table => $field) {
			foreach ($this->validate as $field_name => $validators) {
				foreach ($validators as $validator) {
					if (isset ($validator[0])) {
						if (method_exists($this, $validator[0])) {
							if (isset ($data[$table][$field_name]) && !call_user_func(array (
									$this,
									$validator[0]
								))) {
								if (!isset ($errors[$field_name])) {
									$errors[$field_name] = isset ($validator[1]) ? $validator[1] : 1;
								}
							}
						} else {
							if (isset ($data[$table][$field_name]) && !preg_match($validator[0], $data[$table][$field_name])) {
								if (!isset ($errors[$field_name])) {
									$errors[$field_name] = isset ($validator[1]) ? $validator[1] : 1;
								}
							}
						}
					}
				}
			}
		}
		$this->validationErrors = $errors;
		return $errors;
	}
}
?>